package com.Txunda.fxw.ui.fgt;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.Txunda.fxw.R;
import com.Txunda.fxw.base.utils.util.JSONUtils;
import com.Txunda.fxw.base.utils.util.ListUtils;
import com.Txunda.fxw.ui.activity.home.VideoPlayAty;
import com.Txunda.fxw.ui.adapter.SmallSizedAdapter;
import com.Txunda.fxw.ui.base.BaseFgt;
import com.Txunda.fxw.ui.http.Company;
import com.Txunda.fxw.ui.http.Index;
import com.Txunda.fxw.ui.toolkit.util.PreferenceUtil;
import com.itheima.pulltorefreshlib.PullToRefreshBase;
import com.itheima.pulltorefreshlib.PullToRefreshListView;
import com.kongzue.baseokhttp.HttpRequest;
import com.kongzue.baseokhttp.listener.ResponseListener;
import com.kongzue.baseokhttp.util.Parameter;

import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
@SuppressLint("ValidFragment")
public class SmallSizeFgt extends BaseFgt {
    public String id = "";
    //    PullToRefreshListView fgtSmallLv;
//    private ImageView noDataIv;
    Unbinder unbinder;
    @BindView(R.id.fgt_small_lv)
    PullToRefreshListView fgtSmallLv;
    @BindView(R.id.no_data_iv2)
    ImageView noDataIv2;
    Unbinder unbinder1;
    private String lat = "";
    private String lnt = "";
    private Index index;
    private int p = 1;
    private Company company;
    private SmallSizedAdapter smallSizedAdapter;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    smallSizedAdapter.clear();
                    break;
                case 2:
                    break;
            }
        }
    };

    @SuppressLint("ValidFragment")
    public SmallSizeFgt(String id) {
        this.id = id;
    }


    @Override
    public void onResume() {
        super.onResume();
        p = 1;
        getCompanyList();
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fgt_small_size;
    }

    @Override
    protected void initialized() {
        initData();
        company = new Company();
        getCompanyList();
        lat = PreferenceUtil.readString(getContext(), PreferenceUtil.sp_lat);
        lnt = PreferenceUtil.readString(getContext(), PreferenceUtil.sp_lnt);
    }

    @Override
    protected void requestData() {

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        fgtSmallLv.setEmptyView(noDataIv2);
        company = new Company();
        smallSizedAdapter = new SmallSizedAdapter(getContext());
        fgtSmallLv.setAdapter(smallSizedAdapter);
        fgtSmallLv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                p = 1;
                getCompanyList();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                p++;
                getCompanyList();
            }
        });

        fgtSmallLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                int pos = i - 1;
                String url = smallSizedAdapter.getItem(pos).get("url");
                String title = smallSizedAdapter.getItem(pos).get("name");
                String com_id = smallSizedAdapter.getItem(pos).get("com_id");
                Log.d("zzzzzzzzz", "onItemClick: ========="+url);
//                Bundle bundle = new Bundle();
//                bundle.putString("url", url);
//                bundle.putString("title", title);
//                bundle.putString("com_id", com_id);
                Intent intent = new Intent(getContext(), VideoPlayAty.class);
                intent.putExtra("url",url);
                intent.putExtra("title", title);
                intent.putExtra("com_id", com_id);
                startActivity(intent);
            }
        });

    }

    private void initData() {
        index = new Index();
    }

    private void loadMessage() {
        final String district_id = "";
        final String city_id = "";
        final String province_id = "";
        HttpRequest.POST(getContext(), "http://www.yiyuanchazuo.com/index.php/Api/Company/companyList", new Parameter()
                        .add("lat", lat)
                        .add("lnt", lnt)
                        .add("p", p + "")
                        .add("class_id", id)
                        .add("district_id", district_id)
                        .add("city_id", city_id)
                        .add("province_id", province_id)
                , new ResponseListener() {
                    @Override
                    public void onResponse(String response, Exception error) {
                        if (error == null) {
                            Map<String, String> map = JSONUtils.parseKeyAndValueToMap(response);
                            companyListData(map);
                        }
                    }
                });
    }

    // 商家列表
    private void companyListData(Map<String, String> map) {
        List<Map<String, String>> company_list_data = JSONUtils.parseKeyAndValueToMapList(map.get("data"));
        smallSizedAdapter.clear();
//        smallSizedAdapter.updata(company_list_data);
        if (!ListUtils.isEmpty(company_list_data)) {
            smallSizedAdapter.updata(company_list_data);
        }
        fgtSmallLv.onRefreshComplete();
    }

    /**
     * 获取商家列表
     */
    private void getCompanyList() {
        String district_id = "";
        String city_id = "";
        String province_id = "";
        company.companyList(lat, lnt, p, id, district_id, city_id, province_id, this);
    }

    @Override
    public void onComplete(String requestUrl, String jsonStr) {
        super.onComplete(requestUrl, jsonStr);
        Log.d("zdl", "================" + jsonStr);
        Map<String, String> map = JSONUtils.parseKeyAndValueToMap(jsonStr);
        if (requestUrl.contains("companyList")) { // 商家列表
            companyListData(map);
        }
    }
}